package com.qs.controller;


import cn.hutool.core.lang.tree.Tree;
import com.qs.annotation.OperationLog;
import com.qs.pojo.dto.AddressDto;
import com.qs.pojo.entity.Address;
import com.qs.response.Response;
import com.qs.service.IAddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-11-21
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {

    private final IAddressService addressService;

    /**
     * 查询用户的全部收获地址
     * @param
     * @return
     */
    @GetMapping("/list/{userId}")
    public Response list(@PathVariable("userId") Integer userId){
        log.info("查询用户的全部收获地址请求进来了-------");
        List<AddressDto> addressList = addressService.getList(userId);
        return Response.success(addressList);
    }

    /**
     * 获取省市区数据
     * @param
     * @return
     */
    @GetMapping("/area/list")
    public Response getAllAreaData(){
        log.info("获取省市区数据请求进来了-------");
        List<Tree<String>> areaData = addressService.getAllAreaData();
        return Response.success(areaData);
    }

    /**
     * 根据地址id查询
     * @param
     * @return
     */
    @GetMapping("/list/id/{addressId}")
    public Response getAddressById(@PathVariable("addressId") Integer addressId){
        log.info("根据地址id查询请求进来了-------");
        AddressDto addressDto = addressService.getAddressById(addressId);
        return Response.success(addressDto);
    }

    /**
     * 添加地址方法
     * @param address
     * @return
     */
    @PostMapping("/save")
    public Response saveAddress(@RequestBody()Address address){
        log.info("添加地址方法请求进来了-------");
        addressService.saveAddress(address);
        return Response.success();
    }

    /**
     * 修改地址方法
     * @param address
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody()Address address){
        log.info("修改地址方法请求进来了-------");
        addressService.updateAddress(address);
        return Response.success();
    }

    /**
     * 删除地址方法
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public Response delete(@PathVariable("id") Integer id){
        log.info("删除地址方法请求进来了-------");
        addressService.removeById(id);
        return Response.success();
    }

    /**
     * 用户收获地址回显
     * @param id
     * @return
     */
    @GetMapping("/echo/{id}")
    @OperationLog(operModul = "地址管理-修改用户收货地址",operDesc = "用户收获地址回显")
    public Response echo(@PathVariable("id") Integer id){
        log.info("用户收获地址回显请求进来了-------");
        Address address = addressService.echoById(id);
        return Response.success(address);
    }
}
